
//
//  VEMyBaseViewController.m
//  VEMoneyPorject
//
//  Created by 陈建 on 2018/9/4.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEMyBaseViewController.h"
#import "MyPhotoHeaderTableViewCell.h"
#import "MainLiveAlertUserClassNumView.h"
#import "DTLogInViewController.h"
#import "MyBannerloadTableViewCell.h"
#import "MyTapSharerClassTableViewCell.h"
#import "MyFistReuseTableViewCell.h"
#import "MySingleLableTableViewCell.h"
#import "MySigleInfomationViewController.h"
#import "MyResultMoneyViewController.h"
#import "MyGetMoneyViewController.h"
#import "VEDreweithViewController.h"
@interface VEMyBaseViewController ()
@property (nonatomic, strong) NSMutableArray *expandArray;
/**
 0 上线中，1 已上线
 */
@property (nonatomic, strong) NSString *onlineSatus;
@end

@implementation VEMyBaseViewController

- (NSMutableArray *)expandArray{
    if (!_expandArray) {
        _expandArray = [NSMutableArray array];
        for (NSInteger i =0; i< 3; i++) {
            [_expandArray addObject:@(NO)];
        }
    }
    return _expandArray;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/Getstatus.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"Page":@"-1"}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        NSDictionary *userInfo = response[0];
        self.onlineSatus =  [NSString stringWithFormat:@"%@",userInfo[@"onlineStaues"]];
//        self.onlineSatus = @"1";
        //获取个人信息
        if ([CJUserInfoManager sharedManager].isLogin) {
            [self reuqestUserInfo];
        }else{
            self.isSuccessNet = YES;
            [self cj_reloadData];
        }
        
    }];
  
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)reuqestUserInfo{

    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:[NSString stringWithFormat:@"%@/VEService/Login.do",kPublicInterfaceAPI] isPost:YES withParameters:@{@"Telephone":[[CJUserInfoManager sharedManager] currentUserInfo].TELEPHONE,@"vecode":@""}];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        self.isSuccessNet = YES;
        NSLog(@"%@",response);
        NSDictionary *userInfo = response[0];
         UserInfoModel *model = [UserInfoModel modelWithDictionary:userInfo];
        [[CJUserInfoManager sharedManager] resetUserInfoWithUserInfo:model];
        NSLog(@"%@",[CJUserInfoManager sharedManager].currentUserInfo.STATE);
        [self cj_reloadData];
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.bounces = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(Height_StatusBar, 0,0, 0);
    // Do any additional setup after loading the view.
}


- (NSInteger)cj_numberOfSections{
    return 6;
}

- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    if (section > 1 && section < 5) {
        return 1;
    }
    if (section == 5) {
        if ([self.onlineSatus isEqualToString:@"1"]) {
            
            return [self sevArr].count;
        }else{
            return [self sevArr1].count;
        }
       
    }
    return 1;
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0&&indexPath.row == 1) {
        return [MainLiveAlertUserClassNumView userInfoViewHeight];
    }
    if (indexPath.section > 1 && indexPath.section < 5) {
        BOOL isExp = [self.expandArray[indexPath.section - 2] boolValue];
        if (isExp) {
            return UITableViewAutomaticDimension;
        }else{
            return 0;
        }
    }
    return UITableViewAutomaticDimension;
}

- (UIView *)cj_headerAtSection:(NSInteger)section{
    if (section > 1 && section < 5) {
        MyTapSharerClassTableViewCell *cell = [MyTapSharerClassTableViewCell nibHeaderFooterViewWithTableView:self.tableView];

        if (section == 2) {
            cell.lineView.hidden = YES;
        }else{
            cell.lineView.hidden = NO;
        }
        if ([self.onlineSatus isEqualToString:@"1"]) {
            
            cell.titleLable.text = [self titleArray][section-2];
            cell.biziImageView.image = IMG(@"币子");
        }else{
            cell.titleLable.text = [self titleArray1][section-2];
            cell.biziImageView.image = IMG(@"");
        }
       
        [cell.shareBtn setTitle:[NSString stringWithFormat:@"%@",[self detailArray][section - 2]] forState:UIControlStateNormal];
        cell.TapShareBtnBlock = ^(UIButton *btn) {
            if (![self isLog]) {
                [self pushNoLogVC];
            }else{
                if (section == 2) {
                    
                    NSLog(@"%@",[CJUserInfoManager sharedManager].currentUserInfo.VECODE);
                    NSString *url = [NSString stringWithFormat:@"%@/down.html?VECODE=%@",kPublicInterfaceAPI,[CJUserInfoManager sharedManager].currentUserInfo.VECODE];
                    [[shareFunctionCJ shareFunction] shareInfoP:self.view viewController:self WithUrlSTr:url WithTitle:@"好友分享" withImageViewUrlStr:nil withDetailTitleString:@"邀请好友奖励+500" withType:0];
                    [shareFunctionCJ shareFunction].ShareWXSuccessBlock = ^{
                        NSLog(@"");
                    };
                }else{
                    [CJUIKit showWithString:@"请到对应的专栏查看分享"];
                }
                
                
            }
        };
        cell.TapLaBtnBlock = ^(UIButton *btn) {
            BOOL isExp = [self.expandArray[section - 2] boolValue];
            if (isExp) {
                [self.expandArray replaceObjectAtIndex:(section -2) withObject:@(NO)];
            }else{
                [self.expandArray replaceObjectAtIndex:(section -2) withObject:@(YES)];
            }
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
        };
        return cell;
    }
    return nil;
}

- (CGFloat)cj_sectionHeaderHeightAtSection:(NSInteger)section{
    if (section > 1 && section < 5) {
        return 80;
    }
    return 0;
}

- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MyPhotoHeaderTableViewCell *cell = [MyPhotoHeaderTableViewCell nibWithTableView:self.tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
             cell.photoImageView.layerCornerRadius = 30;
            
            if ([self isLog]) {
                [cell.photoImageView setImageWithUrlString:[CJUserInfoManager sharedManager].currentUserInfo.PHOTO palceHolderImage:[UIImage imageNamed:@"头像"]];
                cell.titlelable.text = [CJUserInfoManager sharedManager].currentUserInfo.USERNAME;
                cell.detailLable.text = @"祝您早日实现财务自由";
            }else{
                if ([self.onlineSatus isEqualToString:@"1"]) {
                    cell.photoImageView.image = [UIImage imageNamed:@"头像"];
                    cell.titlelable.text = @"登录即送2888糖果";
                    cell.detailLable.text = @"已有100万人在挖矿";
                }else{
                    
                    cell.photoImageView.image = [UIImage imageNamed:@"头像"];
                    cell.titlelable.text = @"登录";
                    cell.detailLable.text = @"";
                }
                
            }
            return cell;
        }
        if (indexPath.row == 1) {
            MainLiveAlertUserClassNumView *cell = [MainLiveAlertUserClassNumView cellWithTableView:self.tableView];
            if ([self isLog]) {
                
                [cell setUpUserClassNumWith:@[[NSString stringWithFormat:@"%ld",(long)[CJUserInfoManager sharedManager].currentUserInfo.TODAYTANGGUO],
                                              [NSString stringWithFormat:@"%ld",(long)[CJUserInfoManager sharedManager].currentUserInfo.COINNUMBER],
                                              [CJUserInfoManager sharedManager].currentUserInfo.HEYUESHOUYI,
                                              [NSString stringWithFormat:@"¥%ld",(long)[CJUserInfoManager sharedManager].currentUserInfo.ACCOUNTBALANCE]]];
            }else{
                if ([self.onlineSatus isEqualToString:@"1"]) {
                    [cell setUpUserClassNumWith:@[@"2888",@"2888",@"2888",@"¥0.00"]];

                }else{
                    [cell setUpUserClassNumWith:@[@"0",@"0",@"0",@"¥0.00"]];
                }
                
            }
            cell.TapClassNumBtnBlock = ^(NSInteger index) {
                if ([self isLog]) {
                    if (index == 1) {
                        MyGetMoneyViewController *vc = [[MyGetMoneyViewController alloc] init];
                        vc.title = @"收益记录";
                        [self pushVc:vc];
                    }
                    if (index == 3) {
                        MyResultMoneyViewController *vc = [[MyResultMoneyViewController alloc] init];
                        [self pushVc:vc];
                    }
                   
                    
                    
                }else{
                    [self pushNoLogVC];
                }
            };
            return cell;
        }
    }
    if (indexPath.section == 1) {
        MyBannerloadTableViewCell *cell = [MyBannerloadTableViewCell nibWithTableView:self.tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([self.onlineSatus isEqualToString:@"1"]) {
            cell.plIMageView.image = IMG(@"my_bpl");
            cell.titleLable.text = @"如何挖矿获得VE财经糖果";
        }else{
            cell.plIMageView.image = IMG(@"pxa");
            cell.titleLable.text = @"欢迎使用VE财经";
           
        }
        return cell;
    }
   
    if (indexPath.section > 1 && indexPath.section < 5) {
        MySingleLableTableViewCell *cell = [MySingleLableTableViewCell nibWithTableView:self.tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([self.onlineSatus isEqualToString:@"1"]) {
            cell.titleLable.text = [self titleDescArray][indexPath.section - 2];
        
        }else{
            cell.titleLable.text = [self titleDescArray1][indexPath.section - 2];
        }
        
        return cell;
    }
    if (indexPath.section == 5) {
        MyFistReuseTableViewCell *cell = [MyFistReuseTableViewCell nibWithTableView:self.tableView];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.lineView.hidden = NO;
        if (indexPath.row == 0) {
            cell.topConstant.constant = 10;
            cell.bottomConstant.constant = 0;
        }else{
            cell.topConstant.constant = 0;
            if (indexPath.row == [self sevArr].count -1) {
                cell.bottomConstant.constant = 10;
                cell.lineView.hidden = YES;
            }
        }
        
        if ([self.onlineSatus isEqualToString:@"1"]) {
            cell.titleLable.text = [self sevArr][indexPath.row];
        }else{
            cell.titleLable.text = [self sevArr1][indexPath.row];
            
        }
        
        
        return cell;
    }

    return nil;
}

- (NSArray *)sevArr{
    return @[@"提现管理",@"联系我们"];
}

- (NSArray *)sevArr1{
    return @[@"联系我们"];
}

- (NSArray *)titleArray{
    NSArray *arr = [NSArray array];
    arr = @[@"邀请好友+500",@"分享资讯与快讯+50",@"阅读资讯+10"];
    return arr;
}
- (NSArray *)titleArray1{
    NSArray *arr = [NSArray array];
    arr = @[@"邀请好友",@"分享资讯与快讯",@"阅读资讯"];
    return arr;
}

- (NSArray *)titleDescArray{
    return @[@"邀请好友赠送500个糖果",@"阅读内容并分享",@"认真阅读资讯"];
}
- (NSArray *)titleDescArray1{
    return @[@"邀请好友",@"阅读内容并分享",@"认真阅读资讯"];
}

- (NSArray *)detailArray{
    NSArray *arr = @[@"立即邀请",@"立即分享",@"立即阅读"];
    return arr;
}

- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if ([self isLog]) {
                [self pushConfigerUserInfoViewController];
            }else{
                [self pushNoLogVC];
            }
        }
    }
    if (indexPath.section == 5) {
        if ([self.onlineSatus isEqualToString:@"1"]) {
            if (indexPath.row == 0) {
                if ([self isLog]) {
                    VEDreweithViewController *vc = [VEDreweithViewController new];
                    [self pushVc:vc];
                }else{
                    [self pushNoLogVC];
                }
               
            }
            if ( indexPath.row == 1) {
                [CJUIKit showWithString:@"请联系QQ:421468799"];
            }
        }else{
            [CJUIKit showWithString:@"请联系QQ:421468799"];
           
        }
       

    }
}

- (void)pushConfigerUserInfoViewController{
    MySigleInfomationViewController *vc = [[MySigleInfomationViewController alloc] init];
    vc.title = @"个人信息";
    vc.logOutSuccessBlock = ^{
        [self cj_reloadData];
    };
    [self pushVc:vc];
}
- (void)pushNoLogVC{
    DTLogInViewController *vc = [[DTLogInViewController alloc] init];
    vc.loginSuccessBlock = ^{
        [self cj_reloadData];
    };
    [self pushVc:vc];
    
}

- (BOOL)isLog{
    return [CJUserInfoManager sharedManager].isLogin;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
